import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地mock

/**
 * 获取资产信息变更详情
 * @param getDetailCallback
 * @returns
 */
const getDetailService = (getDetailCallback: (res: any) => void) => {
  const getDetail = useRequest(
    (id: any) => {
      let personUrl = `${baseUrl}/assetApplyChange/detail/${id}/${getUserID()}`;

      return requestFunComponFiex('GET', personUrl, {});
    },
    {
      manual: true,
      onSuccess: (res) => {
        getDetailCallback(res);
      },
    },
  );
  return {
    getDetailRun: getDetail?.run,
    getDetailLoading: getDetail?.loading,
  };
};

/**
 * 添加资产信息变更申请
 * @param addAssetChangeCallback
 * @returns
 */
const addAssetChangeService = (addAssetChangeCallback: (res: any, callType: boolean) => void) => {
  const addAssetChange = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyChange/add`;
      let bodyParams = {
        ...params,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        applyType: '1',
        state: `${getTenantID()}${ILoadMapTypeAll.ASSET_CHANGE_STATUS}1000`, // 起草状态
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        addAssetChangeCallback(res, true);
      },
    },
  );

  return {
    addAssetChangeRun: addAssetChange?.run,
    addAssetChangeLoading: addAssetChange?.loading,
  };
};

/**
 * 编辑资产信息变更申请
 * @param editAssetChangeCallback
 * @returns
 */
const editAssetChangeService = (editAssetChangeCallback: (res: any, callType: boolean) => void) => {
  const editAssetChange = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyChange/edit`;
      let bodyParams = {
        ...params,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res) => {
        editAssetChangeCallback(res, false);
      },
    },
  );

  return {
    editAssetChangeRun: editAssetChange?.run,
    editAssetChangeLoading: editAssetChange?.loading,
  };
};

/** 资产信息变更审批通过后调用 */
const approvedMaintenanceSerivice = (Callback: (res) => void) => {
  const request = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyChange/approved`;

      return requestFunComponFiex('POST', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: Callback,
    },
  );
  return {
    approvedMaintenanceRun: request?.run,
    approvedMaintenanceLoading: request?.loading,
  };
};

/**
 * 审批通过后完成按钮接口
 * @param getFinishCallback
 * @returns
 */
const getFinishService = (getFinishCallback: (res: any) => void) => {
  const getFinish = useRequest(
    (payload: any) => {
      let params = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };

      let reqUrl = `${baseUrl}/assetApplyChange/complete`;

      return requestFunComponFiex('PUT', reqUrl, params);
    },
    {
      manual: true,
      onSuccess: getFinishCallback,
    },
  );
  return {
    getFinishRun: getFinish?.run,
    getFinishLoading: getFinish?.loading,
  };
};

/**
 * 删除申请
 * @param callback 接口回调
 * @returns
 */
const deleteApplyManageService = (callback: (res: any, clickType: boolean) => void) => {
  const deleteApplyManage = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/assetApplyChange/cancel`;
      let bodyParams = {
        ...params,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    deleteApplyManageRun: deleteApplyManage?.run,
    deleteApplyManageLoading: deleteApplyManage?.loading,
  };
};

export { addAssetChangeService, editAssetChangeService, getDetailService, approvedMaintenanceSerivice, getFinishService, deleteApplyManageService };
